var vm = new Vue({
    el: '#contentContainer',
    data: {
        form: {
            name: '',
            type: '0',
            parentIdList: [0],
            url: '',
            permission: '',
            priority: 0,
            iconClass: ''
        },
        rules: {
            name: [
                {required: true, message: '请输入资源名', trigger: 'blur'},
                {min: 0, max: 50, message: '长度不能超过50个字符', trigger: 'blur'}
            ],
            type: [
                {required: true, message: '请选择资源类型', trigger: 'change'}
            ],
            parentIdList: [
                {type: 'array', required: true, message: '请选择父资源', trigger: 'change'}
            ],
            permission: [
                {required: true, message: '请输入资源标识', trigger: 'blur'},
                {min: 0, max: 50, message: '长度不能超过100个字符', trigger: 'blur'}
            ],
            url: [
                {required: true, message: '请输入URL', trigger: 'blur'},
                {min: 0, max: 255, message: '长度不能超过255个字符', trigger: 'blur'}
            ]
        },
        resourceList: [],
        options: [{id: 0, text: '根节点'}],
        props: {
            children: 'children',
            label: 'text',
            value: 'id'
        }
    },
    created: function () {
        var $this = this;

        Common.getJson('/sys/role/getResourceTreeList', {},
            function (response) {
                if (!response || response.status != 200)
                    return;

                var data = response.result;

                $this.resourceList = data;
            });
    },
    methods: {
        submitForm: function (formName) {
            var $this = this;

            $this.$refs[formName].validate(function (valid) {
                if (!valid)
                    return false;

                Common.postJson('/sys/resource/add', $this.form,
                    function (response) {
                        if (!response)
                            return;

                        //保存成功
                        if (response.status == 200 && response.result.success)
                            window.location.href = '/sys/resource/list';
                        else {
                            var errMsg = response.status == 200 ? response.result.errMsg : response.msg;

                            $this.$notify.error({
                                title: '错误',
                                message: errMsg,
                                offset: $("#main", window.parent.document).height() - 120,
                                duration: 0
                            });
                        }
                    });
            });
        },
        resetForm: function (formName) {
            this.$refs[formName].resetFields();
        },
        onTypeChange: function (type) {
            this.form.parentIdList = [];

            if (type === '0') {
                this.options = [{id: 0, text: '根节点'}];
                this.form.parentIdList = [0];
                return;
            }

            if (type === '1') {
                this.options = [];
                for (var i = 0; i < this.resourceList.length; i++) {
                    var resource = this.resourceList[i];
                    if (resource.type != 0)
                        continue;

                    var node = {id: resource.resourceId, text: resource.name};
                    this.options.push(node);
                }

                return;
            }

            if (type === '2') {
                this.options = [];
                for (var i = 0; i < this.resourceList.length; i++) {
                    var resource = this.resourceList[i];
                    if (resource.type === 2)
                        continue;

                    if (resource.type === 0) {
                        var node = {id: resource.resourceId, text: resource.name, children: []};
                        this.options.push(node);
                    }

                    if (resource.type === 1) {
                        var subNode = {id: resource.resourceId, text: resource.name};
                        for (var j = 0; j < this.options.length; j++) {
                            if (resource.parentId === this.options[j].id) {
                                this.options[j].children.push(subNode);
                            }
                        }
                    }
                }

                return;
            }
        }
    }
});